package gw.com.android.ui.dialog;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.Timer;
import java.util.TimerTask;

import gw.com.android.app.AppContances;
import www.com.library.app.Logger;

/**
 * 订单提示弹层类
 * @author reeta
 *
 */
public class OrderPopWindow {
	protected Activity activity;
	public PopupWindow popWindow = null;// pop对象
	protected TextView mTitleView;
	protected View mRootView = null;// 绑定显示视图的view
	protected View mView;//弹层布局视图
	protected static OrderPopWindow mInstance = null;

	public static OrderPopWindow instance(){
		return mInstance;
	}

	/**
	 * 初始化pop界面元素和数据
	 * 
	 */
	public void initPopWindow(Activity activity, View rootView) {
		mInstance = this;
		this.activity = activity;
		this.mRootView = rootView;
		final LayoutInflater inflater = LayoutInflater.from(activity);
		final View dialogView = inflater.inflate(R.layout.order_tips_bar, null);

		mView = dialogView.findViewById(R.id.rl_order_hint);
		mView.getBackground().setAlpha(80);
		mTitleView = (TextView) dialogView.findViewById(R.id.tv_order_hint);

		popWindow = new PopupWindow(dialogView,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		//by reeta 此处必须设置，否则点击事件无效，选择不了
		popWindow.setBackgroundDrawable(ContextCompat.getDrawable(activity, R.color.color_g));

		popWindow.setOutsideTouchable(false);

		//为了设置返回按钮关闭弹层
		popWindow.setFocusable(false);

		mView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				hidden();
			}
		});

	}

	/**
	 * 设置弹框的值
     */
	public void setTitleView(String mTitle){
        mTitleView.setText(mTitle);
	}
	
	/**
	 * 返回当前pop是否显示状态
	 * 
	 */
	public boolean hasShowing() {
		 return  null == popWindow ? false : popWindow.isShowing();
	}

	/**
	 * 隐藏pop界面
	 * 
	 */
	public void hidden() {
		
		mInstance = null;

		if(null != task){
			task.cancel();
			task = null;
		}

		if(null != timer){
			timer.cancel();
			timer = null;
		}
		
		if (null == popWindow) {
			return;
		}

		popWindow.dismiss();
	}

	/**
	 * 显示pop界面
	 * 
	 */
	public void show() {
		try {
			if(!activity.isFinishing()){
				if(null == mRootView){
					mRootView = activity.getWindow().getDecorView();
					int top = (int) (activity.getResources().getDimension(R.dimen.height_a) + activity.getResources().getDimension(R.dimen.height_c)
							- activity.getResources().getDimension(R.dimen.space_e));
					popWindow.showAtLocation(mRootView, Gravity.TOP, 0, top);
				}else{
					popWindow.showAsDropDown(mRootView, 0, 0);
				}
				initTimerTask();
			}
		}catch (WindowManager.BadTokenException e){
		    Logger.e("当前activity还未绘制好视图");
	    }
	}

	/**定时器*/
	private Timer timer = new Timer();
	private TimerTask task;


	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == AppContances.HANDLER_TIMER_TASK){
				hidden();
			}
		}
	};

	/**
	 * 初始化定时任务
	 */
	public void initTimerTask(){
		// 初始化定时器
		task = new TimerTask() {
			@Override
			public void run() {
				handler.sendEmptyMessage(AppContances.HANDLER_TIMER_TASK);
			}
		};
		// 启动定时器,200ms后关闭提示
		timer.schedule(task, AppContances.TIMER_ORDER_TASK_TIME);
	}
}
